<template>
  <ych-modal
    :fullscreen="false"
    :title="title"
    :visible="visible"
    @cancel="close">

    <a-form>
      <a-form-item label='授权角色'>

        <a-tree
          v-if="treeData.length>0"
          checkable
          @check="onCheck"
          :checkedKeys="checkedKeys"
          :treeData="treeData"
          @expand="onExpand"
          @select="onTreeNodeSelect"
          :selectedKeys="selectedKeys"
          :expandedKeys="expandedKeysss"
          :checkStrictly="checkStrictly">

        </a-tree>
        <div v-else><h3>无可配置角色!</h3></div>
      </a-form-item>
    </a-form>

    <template slot="footer">
      <a-popconfirm key="close" title="确定放弃编辑？" @confirm="close" :okText="$t('m.common.button.ok')" :cancelText="$t('m.common.button.cancel')">
        <a-button>{{$t('m.common.button.cancel')}}</a-button>
      </a-popconfirm>
      <a-button key="ok" type="primary" @click="handleSubmit(true)">保存并关闭</a-button>
    </template>

  </ych-modal>

</template>
<script>

  import {httpAction, getAction} from '@/api/manage';
  import YchConstant from "@/api/YchConstant";

  export default {
    name: "PortletRoleAuthModal",
    components:{

    },
    data(){
      return {
        portletCode:"",
        treeData: [],
        defaultCheckedKeys:[],
        checkedKeys:[],
        halfCheckedKeys:[],
        expandedKeysss:[],
        allTreeKeys:[],
        autoExpandParent: true,
        checkStrictly: true,
        title:"门户配置授权",
        visible: false,
        loading: false,
        selectedKeys:[],
        url: {
          queryPortletRolesByCode: YchConstant.PREFIX_SYSTEM + "/portlet/queryPortletRolesByCode",
          savePortletRole:YchConstant.PREFIX_SYSTEM + "/portlet/savePortletRole"
        },
      }
    },
    methods: {
      switchCheckStrictly (v) {
        if(v==1){
          this.checkStrictly = false
        }else if(v==2){
          this.checkStrictly = true
        }
      },
      onTreeNodeSelect(id){
        if(id && id.length>0){
          this.selectedKeys = id
        }
        this.$refs.datarule.show(this.selectedKeys[0],this.departId,this.roleId)
      },
      onCheck (o) {
        if(this.checkStrictly){
          this.checkedKeys = o.checked;
        }else{
          this.checkedKeys = o
        }
      },
      show(portletCode){
        this.portletCode = portletCode
        this.visible = true;
      },
      close () {
        this.reset()
        this.$emit('close');
        this.visible = false;
      },
      onExpand(expandedKeys){
        this.expandedKeysss = expandedKeys;
        this.autoExpandParent = false
      },
      reset () {
        this.expandedKeysss = []
        this.checkedKeys = []
        this.defaultCheckedKeys = []
        this.loading = false
      },
      expandAll () {
        this.expandedKeysss = this.allTreeKeys
      },
      closeAll () {
        this.expandedKeysss = []
      },
      checkALL () {
        this.checkedKeys = this.allTreeKeys
      },
      cancelCheckALL () {
        this.checkedKeys = []
      },
      handleCancel () {
        this.close()
      },
      handleSubmit(exit) {
        let that = this;
        let params =  {
          portletCode:that.portletCode,
          roleIds:that.checkedKeys.join(","),
        };
        that.loading = true;
        httpAction(this.url.savePortletRole, params, "post").then((res) => {
          if (res.success) {
            that.messageUtil.success(res.message);
            that.loading = false;
            if (exit) {
              that.close()
            }
          } else {
            that.messageUtil.error(res.message);
            that.loading = false;
            if (exit) {
              that.close()
            }
          }
        }).finally(() => {
          that.loading = false;
          that.close();
        })
      },
      convertTreeListToKeyLeafPairs(treeList, keyLeafPair = []) {
        for(const {key, isLeaf, children} of treeList) {
          keyLeafPair.push({key, isLeaf})
          if(children && children.length > 0) {
            this.convertTreeListToKeyLeafPairs(children, keyLeafPair)
          }
        }
        return keyLeafPair;
      },
      loadData(){
        getAction(this.url.queryPortletRolesByCode, {
          portletCode: this.portletCode
        }).then((res) => {
          this.treeData = res.result.allRoles
          this.allTreeKeys = res.result.ids
          this.checkedKeys = [...res.result.authRoles];
          this.defaultCheckedKeys = [...res.result.authRoles];
          this.expandedKeysss = this.allTreeKeys;
        });
      }
    },
    watch: {
      visible () {
        if (this.visible ) {
          this.loadData();
        }
      }
    }
  }

</script>
<style lang="less" scoped>
  .drawer-bootom-button {
    position: absolute;
    bottom: 0;
    width: 100%;
    border-top: 1px solid #e8e8e8;
    padding: 10px 16px;
    text-align: right;
    left: 0;
    background: #fff;
    border-radius: 0 0 2px 2px;
  }

</style>
